﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading;
using Axe.Threading;

namespace Axe.Collections
{
	public class EachCollection<T> : Collection<T>, IEquatable<EachCollection<T>>
	{

        public EachCollection()
        {
        }

        public EachCollection(IEnumerable<T> items)
        {
            AddRange(items);
        }

        public void ForEachMT(Action<T> action)
        {
            new ParallelForEach<T>(this).Run(action);
        }

        public void ForEach(Action<T> action)
        {
            foreach (T obj in this)
            {
                action(obj);
            }
        }

        public void AddRange(IEnumerable<T> items)
        {
            if (items != null)
            {
                foreach (T item in items)
                {
                    Add(item);
                }
            }
        }

        public void AddRange(params T[] items)
        {
            if (items != null)
            {
                foreach (T item in items)
                {
                    Add(item);
                }
            }
        }

        #region IEquatable<EachCollection<T>> Members

        public bool Equals(EachCollection<T> other)
        {
            if (Count != other.Count)
                return false;

            for (int i = 0; i < Count; i++)
            {
                T left = this[i];
                T right = other[i];

                if(Utility.TrueEquals<T>(left,right) == false)
                    return false;
            }

            return true;
        }

        #endregion

        public T First
        {
            get { return this[0]; }
        }

        public T Last
        {
            get { return this[this.Count - 1]; }
        }
    }
}
